package com.fowo.api.pda.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fowo.api.common.model.R;
import com.fowo.api.entity.Product;
import com.fowo.api.entity.WarehouseBin;
import com.fowo.api.mapper.ListingFatherMapper;
import com.fowo.api.mapper.ProductMapper;
import com.fowo.api.mapper.WarehouseBinMapper;
import com.fowo.api.model.adjustment.order.AdjustmentOrderVo;
import com.fowo.api.model.adjustment.order.product.AdjustmentOrderProductItemVo;
import com.fowo.api.pda.model.InventoryBo;
import com.fowo.api.pda.model.InventoryPlacementItemVo;
import com.fowo.api.pda.service.PdaInventoryService;
import com.fowo.api.service.AdjustmentOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Tag(name = "PDA库存调整")
@RequestMapping("/api/pda/adjustment-order")
@RestController
public class PdaAdjustmentOrderController {
    @Resource
    private AdjustmentOrderService service;
    @Resource
    private PdaInventoryService pdaInventoryService;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private WarehouseBinMapper warehouseBinMapper;
    @Resource
    private ListingFatherMapper listingFatherMapper;

    @Operation(summary = "查询产品ID")
    @GetMapping("/productId")
    public R<Long> getProductIdBySku(String sku){
        final Product product = productMapper.selectOne(Wrappers.lambdaQuery(Product.class).eq(Product::getSku, sku));
        return R.data(product.getId());
    }

    @Operation(summary = "查询仓位ID")
    @GetMapping("/binId")
    public R<Long> getBinIdByName(String name){
        final WarehouseBin warehouseBin = warehouseBinMapper.selectOne(Wrappers.lambdaQuery(WarehouseBin.class).eq(WarehouseBin::getStorageBin, name));
        return R.data(warehouseBin.getId());
    }

    @Operation(summary = "查询产品库存")
    @GetMapping()
    public R<List<InventoryPlacementItemVo>> getInventoryPlacementItemVo(InventoryBo bo){

        return R.data(pdaInventoryService.getInventoryPlacementItemVo(bo));
    }

    @Operation(summary = "新增")
    @PostMapping
    public R<Long> create(@Validated @RequestBody AdjustmentOrderVo model) {
        model.setStatus("5");
        model.setType("0");
        for(AdjustmentOrderProductItemVo item : model.getProductLists()){
            item.setFnsku(listingFatherMapper.getIdByFnsku(item.getFnskuName()));
        }
        return R.data(() -> service.create(model));
    }
}
